package andy.demo.parseXML;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import andy.demo.R;
import andy.demo.parseXML.dom.service.DOMService;
import andy.demo.parseXML.pull.PULLService;
import andy.demo.parseXML.sax.domain.Person;
import andy.demo.parseXML.sax.service.SAXService;



public class XMLParseActivity extends Activity {
	
	private Button bt_dom;
	private Button bt_sax;
	private Button bt_pull;
	private TextView tv_content;
	private InputStream in = null;
	private ArrayList<Person> personList = new ArrayList<Person>();
	private String file_name;
	
	


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.xml_parse_main);
		
		bt_dom = (Button) this.findViewById(R.id.bt_dom);
		bt_sax = (Button) this.findViewById(R.id.bt_sax);
		bt_pull = (Button) this.findViewById(R.id.bt_pull);
		tv_content = (TextView) this.findViewById(R.id.tv_content);
		
		file_name = "domperson.xml";
		
		bt_dom.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				//get InputStream by Type1
//				in = XMLParseActivity.class.getClassLoader().getResourceAsStream("assets/domperson.xml");
				
				//get InputStream by Type2
				try {
					in = XMLParseActivity.this.getResources().getAssets().open(file_name);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
				personList = DOMService.readXML(in);
				tv_content.setText(personList.toString());
				
			}
		});
		
		bt_sax.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				in = XMLParseActivity.class.getClassLoader().getResourceAsStream("assets/saxperson.xml");
				personList = SAXService.readXML(in);
				tv_content.setText(personList.toString());
				
				
			}
		});
		
		bt_pull.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				in = XMLParseActivity.class.getClassLoader().getResourceAsStream("assets/pullperson.xml");
				personList = PULLService.readXML(in);
				tv_content.setText(personList.toString());
			}
		});
		
		
	}
	
	
}
